package com.bite.bite_forum.services.Impl;

import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.dao.MessageMapper;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.Message;
import com.bite.bite_forum.model.User;
import com.bite.bite_forum.services.IMessageService;
import com.bite.bite_forum.services.IUserService;
import com.bite.bite_forum.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class MessageServiceImpl implements IMessageService {
    @Resource
    MessageMapper messageMapper;
    @Resource
    IUserService userService;

    @Override
    public void create(Message message) {
          //校验
          if(message == null || message.getPostUserId() == null
                  || message.getReceiveUserId()  == null || StringUtils.isEmpty(message.getContent())){
              log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
              throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
          }
          //验证接受者是否存在
          User user = userService.selectByPrimaryKey(message.getReceiveUserId());
          if(user == null || user.getDeleteState() == 1){
              log.info(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
              throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
          }
          //设置默认值
          message.setState((byte) 0);
          message.setDeleteState((byte) 0);
          message.setCreateTime(new Date());
          message.setUpdateTime(new Date());
          int row = messageMapper.insertSelective(message);
            if(row != 1){
                log.info(ResultCode.ERROR_SERVICES.getMessage());
                throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
            }
    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        //校验
        User user = userService.selectByPrimaryKey(receiveUserId);
        if(user == null || user.getDeleteState() == 1){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        return count;
    }

    @Override
    public Message selectById(Long id) {
        if(id == null || id <= 0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);

        return message;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        //校验
        User user = userService.selectByPrimaryKey(receiveUserId);
        if(user == null || user.getDeleteState() == 1){
            log.info(ResultCode.FAILED_USER_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        List<Message> result = messageMapper.selectByReceiveUserId(receiveUserId);
        return result;
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if(id ==null || id <= 0 || state < 0 || state > 2){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //封装对象
        Message message = new Message();
        message.setId(id);
        message.setState(state);
        message.setUpdateTime(new Date());
        int row = messageMapper.updateByPrimaryKeySelective(message);
        if(row != 1){
            log.info(ResultCode.ERROR_SERVICES.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
    }

    @Override
    public void reply(Long repliedId, Message message) {
        //校验参数
        if(repliedId == null || repliedId <= 0){
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        //判断repliedId是否存在
        Message existsMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(existsMessage == null || existsMessage.getDeleteState() == 1){
            log.info(ResultCode.FAILED_MESSAGE_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS));
        }
        //更新状态
        updateStateById(repliedId, (byte) 2);
        //插入数据
        create(message);
    }
}
